package org.example.mydeom.security;

import com.alibaba.fastjson.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.mydeom.result.Result;
import org.example.mydeom.util.ResultUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ResultUtil.Respond("权限不足",accessDeniedException.getMessage(),403, response);
//
//        Result<Object> result = new Result<>();
//        result.setCode(403);
//        result.setMsg("权限不足");
//        result.setData(accessDeniedException.getMessage());
//        String jsonString = JSON.toJSONString(result);
//        response.setContentType("application/json;charset=UTF-8");
//        response.getWriter().write(jsonString);
    }
}
